Passed
Pull Request — master (#75)
by Mathieu
01:51
created

GetEventsOverview.index   C

Complexity

Conditions 9

Size

Total Lines 78
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 78
rs 5.6811
c 0
b 0
f 0
cc 9

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import {Event, EventType} from './Event.entity';
2
import {IEventsOverview} from './IEventsOverview';
3
4
export class GetEventsOverview {
5
  public index(events: Event[]): IEventsOverview {
6
    console.log('inNNNN');
7
    const eventsByDate = [];
8
    const overview: IEventsOverview = {
9
      mission: 0,
10
      dojo: 0,
11
      formationConference: 0,
12
      holiday: 0,
13
      medicalLeave: 0,
14
      support: 0,
15
      workFree: 0,
16
      other: 0,
17
      mealTicket: 0
18
    };
19
20
    for (const event of events) {
21
      const dayIndex = new Date(event.getDate()).getDate() - 1;
22
      const time = event.getTime() / 100;
23
      const type = event.getType();
24
25
      if (eventsByDate[dayIndex]) {
26
        eventsByDate[dayIndex].push({time, type});
27
      } else {
28
        eventsByDate[dayIndex] = [{time, type}];
29
      }
30
31
      switch (event.getType()) {
32
        case EventType.MISSION:
33
          overview.mission += time;
34
          break;
35
        case EventType.DOJO:
36
          overview.dojo += time;
37
          break;
38
        case EventType.FORMATION_CONFERENCE:
39
          overview.formationConference += time;
40
          break;
41
        case EventType.HOLIDAY:
42
          overview.holiday += time;
43
          break;
44
        case EventType.MEDICAL_LEAVE:
45
          overview.medicalLeave += time;
46
          break;
47
        case EventType.SUPPORT:
48
          overview.support += time;
49
          break;
50
        case EventType.WORK_FREE:
51
          overview.workFree += time;
52
          break;
53
        case EventType.OTHER:
54
          overview.other += time;
55
          break;
56
      }
57
    }
58
59
    for (const sortedEvent of eventsByDate) {
60
      if (!sortedEvent) {
61
        continue;
62
      }
63
64
      let totalPerDay = 0;
65
66
      for (const {time, type} of sortedEvent) {
67
        if (
68
          type !== EventType.WORK_FREE &&
69
          type !== EventType.MEDICAL_LEAVE &&
70
          type !== EventType.HOLIDAY &&
71
          type !== EventType.OTHER
72
        ) {
73
          totalPerDay += time;
74
        }
75
      }
76
77
      if (totalPerDay > 0.5) {
78
        overview.mealTicket++;
79
      }
80
    }
81
    console.log(overview);
82
    return overview;
83
  }
84
}
85